# _*_ coding:utf-8 _*_
__author__ = 'nico'
__date__ = '2019/8/14 6:51 PM'

from users.models import EmailVertifyRecord
from random import Random
from django.core.mail import send_mail
from MxOnline.settings import EMAIL_FROM

def send_register_email(email, send_type='register'):
    email_record = EmailVertifyRecord()
    random_str = generate_random_str(16)
    email_record.email = email
    email_record.send_type = send_type
    email_record.code = random_str
    email_record.save()

    email_title = ''
    email_style = ''
    if send_type == 'register':
        email_title = '注册激活'
        email_style = 'active'
    elif send_type == 'forget':
        email_title = '找回密码'
        email_style = 'reset'

    email_boby = '请点击下面的链接激活: http://127.0.0.1:8000/{0}/{1}'.format(email_style, email_record.code)

    send_status = send_mail(email_title, email_boby, EMAIL_FROM, [email])
    if send_status:
        pass


def generate_random_str(randomlength=8):
    str = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQq'
    length = len(chars) - 1
    random = Random()
    for i in range(randomlength):
        str+=chars[random.randint(0, length)]
    return str

